package com.zeratul.common.event.serial;

import com.zeratul.common.event.base.serial.BaseEventSerialization;
import com.zeratul.common.event.domain.ZtEvent;
import com.zeratul.common.json.utils.JsonUtils;

import java.nio.charset.StandardCharsets;


/**
 * 简单的事件序列化器
 */
public class SimpleEventSerialization implements BaseEventSerialization {

    /**
     * 事件序列化
     *
     * @param ztEvent
     */
    @Override
    public <T> byte[] serialize(ZtEvent<T> ztEvent) {
        return JsonUtils.serialize(ztEvent)
                .getBytes(StandardCharsets.UTF_8);
    }

    /**
     * 事件反序列化
     *
     * @param bytes
     */
    @SuppressWarnings("unchecked")
    @Override
    public <T> ZtEvent<T> deserialize(byte[] bytes) {
        String json = new String(bytes, StandardCharsets.UTF_8);
        return JsonUtils.deserialize(json, ZtEvent.class);
    }
}